home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / CBGRX103.ZIP / contrib / libgrx / src / loaddrv.c < prev    next >
Text File  |  1993-12-06  |  6KB  |  220 lines

  1. /**
  2.  ** LOADDRV.C
  3.  **
  4.  **  Copyright (C) 1992, Csaba Biegl
  5.  **    820 Stirrup Dr, Nashville, TN, 37221
  6.  **    csaba@vuse.vanderbilt.edu
  7.  **
  8.  **  This file is distributed under the terms listed in the document
  9.  **  "copying.cb", available from the author at the address above.
  10.  **  A copy of "copying.cb" should accompany this file; if not, a copy
  11.  **  should be available from where this file was obtained.  This file
  12.  **  may not be distributed without a verbatim copy of "copying.cb".
  13.  **  You should also have received a copy of the GNU General Public
  14.  **  License along with this program (it is in the file "copying");
  15.  **  if not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  16.  **  Cambridge, MA 02139, USA.
  17.  **
  18.  **  This program is distributed in the hope that it will be useful,
  19.  **  but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.  **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.  **  GNU General Public License for more details.
  22.  **/
  23.  
  24. #pragma  inline
  25.  
  26. #include "grx.h"
  27. #include "libgrx.h"
  28. #include "grdriver.h"
  29. #include "grxfile.h"
  30. #include "gmalloc.h"
  31.  
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <ctype.h>
  36. #include <dos.h>
  37. #include <fcntl.h>
  38. #include <io.h>
  39.  
  40. static char driver_loaded = FALSE;
  41. static char new_driver = FALSE;
  42.  
  43. static void far dummy_func(void) {}
  44. static GrDriverHeader far *drv;
  45.  
  46. static void far (*set_mode_func)(void) = dummy_func;
  47. static void far (*set_page_func)(void) = dummy_func;
  48. static unsigned short far *option_word_address;
  49.  
  50.  
  51. static char *near get_token(char *p,char *token)
  52. {
  53.     while(*p == ' ') p++;
  54.     while(*p && (*p != ' ')) { *token++ = tolower(*p); p++; }
  55.     *token = '\0';
  56.     return(p);
  57. }
  58.  
  59. static int near find_keyword(char *p,char *token,char *name)
  60. {
  61.     while(*p != '\0') {
  62.         p = get_token(p,token);
  63.         if(strcmp(token,name) == 0) {
  64.         p = get_token(p,token);
  65.         return((*token == '\0') ? FALSE : TRUE);
  66.         }
  67.     }
  68.     return(FALSE);
  69. }
  70.  
  71. static void near load_driver(int errors_fatal)
  72. {
  73.     void far (*drv_init_func)(void);
  74.     char *p,driver[100];
  75.     int  drvfile,test;
  76.     long size;
  77.  
  78.     if((p = getenv("GRXDRV")) == NULL) {
  79.         if(((p = getenv("GO32")) == NULL) ||
  80.            (find_keyword(p,driver,"driver") == FALSE)) {
  81.         if(!errors_fatal) return;
  82.         fputs("No graphics driver specified\n",stderr);
  83.         exit(1);
  84.         }
  85.     }
  86.     else p = get_token(p,driver);
  87.     if((drvfile = open(driver,(O_RDONLY | O_BINARY))) < 0) {
  88.         if(!errors_fatal) return;
  89.         fputs("Graphics driver \"",stderr);
  90.         fputs(driver,stderr);
  91.         fputs("\" not found\n",stderr);
  92.         exit(1);
  93.     }
  94.     size = filelength(drvfile);
  95.     if(!(drv = _GrFarMalloc(size + 16))) {
  96.         if(!errors_fatal) return;
  97.         fputs("No memory for graphics driver\n",stderr);
  98.         exit(1);
  99.     }
  100.     drv = MK_FP((FP_SEG(drv) + ((FP_OFF(drv) + 15) >> 4)),0);
  101.     if(_GrFarRead(drvfile,drv,size) != size) {
  102.         if(!errors_fatal) return;
  103.         fputs("Error loading graphics driver\n",stderr);
  104.         close(drvfile);
  105.         exit(1);
  106.     }
  107.     close(drvfile);
  108.     option_word_address = &drv->driver_flags;
  109.     if(*option_word_address & GRD_NEW_DRIVER) new_driver = TRUE;
  110.     if(find_keyword(p,driver,"tw") && ((test = atoi(driver)) > 0)) drv->def_tw = test;
  111.     if(find_keyword(p,driver,"th") && ((test = atoi(driver)) > 0)) drv->def_th = test;
  112.     if(find_keyword(p,driver,"gw") && ((test = atoi(driver)) > 0)) drv->def_gw = test;
  113.     if(find_keyword(p,driver,"gh") && ((test = atoi(driver)) > 0)) drv->def_gh = test;
  114.     set_mode_func = (void far (*)())MK_FP(FP_SEG(drv),drv->modeset_routine);
  115.     set_page_func = (void far (*)())MK_FP(FP_SEG(drv),drv->paging_routine);
  116.     if(new_driver) {
  117.         if(find_keyword(p,driver,"nc") && ((test = atoi(driver)) > 0))
  118.         drv->def_numcolor = test;
  119.         drv_init_func = (void far (*)())MK_FP(FP_SEG(drv),drv->driver_init_routine);
  120.         asm push di;
  121.         asm push si;
  122.         _BX = FP_OFF((void far *)&drv_init_func);
  123.         _ES = FP_SEG((void far *)&drv_init_func);
  124.         asm push ds;
  125.         asm push WORD  PTR es:[bx+2]
  126.         asm pop  ds;
  127.         asm call DWORD PTR es:[bx];
  128.         asm pop  ds;
  129.         asm pop  si;
  130.         asm pop  di;
  131.         if(_AX == 0) {
  132.         if(!errors_fatal) return;
  133.         /* You may want to do something more appropriate here */
  134.         fputs("Graphics initialization error -- probably incorrect driver\n",stderr);
  135.         exit(1);
  136.         }
  137.     }
  138.     driver_loaded = TRUE;
  139. }
  140.  
  141. long _GrLowSetMode(int mode,int width,int height,int colors)
  142. {
  143.     int retval;
  144.  
  145.     if(!driver_loaded) {
  146.         load_driver((mode >= GR_320_200_graphics) ? TRUE : FALSE);
  147.         if(!driver_loaded) {
  148.         _AX = 3;
  149.         geninterrupt(0x10);
  150.         return(-1);
  151.         }
  152.     }
  153.     if(!new_driver && (mode == 9)) mode = 6;
  154.     asm push di;
  155.     asm push si;
  156.     _SI = FP_OFF((void far *)&set_mode_func);
  157.     _ES = FP_SEG((void far *)&set_mode_func);
  158.     asm mov  bx,colors;
  159.     asm mov  cx,width;
  160.     asm mov  dx,height;
  161.     asm mov  ax,mode;
  162.     asm push ds;
  163.     asm push WORD  PTR es:[si+2]
  164.     asm pop  ds;
  165.     asm call DWORD PTR es:[si];
  166.     asm pop  ds;
  167.     retval = _BX;
  168.     _GrScreenX = _CX;
  169.     _GrScreenY = _DX;
  170.     asm pop  si;
  171.     asm pop  di;
  172.     return(new_driver ? retval : *option_word_address);
  173. }
  174.  
  175. long _GrGetDriverModes(GR_DRIVER_MODE_ENTRY far **t,GR_DRIVER_MODE_ENTRY far **g)
  176. {
  177.     if(!driver_loaded) load_driver(TRUE);
  178.     if(new_driver) {
  179.        *t = MK_FP(FP_SEG(drv),drv->text_table);
  180.        *g = MK_FP(FP_SEG(drv),drv->graphics_table);
  181.     }
  182.     else {
  183.        *t = (GR_DRIVER_MODE_ENTRY far *)NULL;
  184.        *g = (GR_DRIVER_MODE_ENTRY far *)NULL;
  185.     }
  186.     return(drv->driver_flags);
  187. }
  188.  
  189. /*
  190.  * C entry point
  191.  */
  192. void _GrSetVideoPage(int page)
  193. {
  194.     asm push di;
  195.     asm push si;
  196.     _AX = page;
  197.     (*set_page_func)();
  198.     asm pop  si;
  199.     asm pop  di;
  200. }
  201.  
  202. /*
  203.  * ASM entry point: page(s) in AX
  204.  */
  205. void far _GrSetAsmPage(void)
  206. {
  207.     asm push di;
  208.     asm push si;
  209.     asm push dx;
  210.     asm push cx;
  211.     asm push bx;
  212.     (*set_page_func)();
  213.     asm pop  bx;
  214.     asm pop  cx;
  215.     asm pop  dx;
  216.     asm pop  si;
  217.     asm pop  di;
  218. }
  219.  
  220.